#!/usr/bin/python3.8.9
# -*- coding: utf-8 -*-

# @Author  : Tina Yu
# @Time    : 2021-8-17 8:32

import csv
from datetime import datetime

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

font_set = FontProperties(fname=r'c:\windows\fonts\simsun.ttc', size=14)

filename = 'death_valley_2018_simple.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    # 从文件中获取最高温度、最低温度以及对应日期
    dates, highs, lows = [], [], []
    for row in reader:
        current_date = datetime.strptime(row[2], '%Y-%m-%d')
        try:
            high = int(row[4])
            low = int(row[5])
        except ValueError:
            print(f'Missing data for {current_date}')
        else:
            dates.append(current_date)
            highs.append(high)
            lows.append(low)

print(highs)

# 根据最高温度绘制图形
plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.plot(dates, highs, c='red', alpha=0.5)  # alpha指定颜色的透明度，0表示完全透明，1为完全不透明
ax.plot(dates, lows, c='blue', alpha=0.5)
ax.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1)  # fill_between接收1个x值、2个y值，facecolor指定填充区域的颜色

# 设置图形的格式
ax.set_title("2018年每日最高、低温度\n 美国加利福尼亚州死亡谷", fontproperties=font_set, fontsize=24)
ax.set_xlabel("日期", fontproperties=font_set)
fig.autofmt_xdate()
ax.set_ylabel("温度（F）", fontproperties=font_set)
ax.tick_params(axis='both', which='major', labelsize=8)
plt.ylim(10, 130)

plt.show()
